<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    html{
      padding: 0;
      margin: 0;
    }
    #bg{
      position: fixed;
      left: 0;top: 0;
      width: 100%;height: 100%;
      background: pink;
    }
    /* #open{

    } */
    #mask{
      position: absolute;
      left: 50%;top: 50%;
      transform: translate(-50%,-50%);
      display: none;
      width: 300px;height: 300px;
      background:rgba(0,0,0,.2);
      text-align: center;
    }
  </style>
</head>
<body>
    <div id="bg">
      <div id="open">点击打开遮罩层</div>
      <div id="mask">遮罩层</div>
    </div>
  <script lang="javascript">
    const open = document.getElementById('open')
    const bg = document.getElementById('bg')
    const mask =document.getElementById('mask')
    open.onclick= function(e){
      e.stopPropagation()
      mask.style.display = 'block'
    }
    bg.onclick =function(e){
      if(e.target===bg){
         mask.style.display='none'
      }
    }
  </script>
</body>
</html>